package com.shekhar.paas.twitter;

import java.util.Collections;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import twitter4j.Query;
import twitter4j.QueryResult;
import twitter4j.Tweet;
import twitter4j.Twitter;
import twitter4j.TwitterException;

@Service
public class TwitterService {

    private Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    private Twitter twitter;

    public List<Tweet> search(String searchTerm) {
        if (logger.isDebugEnabled()) {
            logger.debug("Quering for search term '" + searchTerm + "'");
        }
        Query query = new Query();
        query.setRpp(100);
        query.setQuery(searchTerm);
        QueryResult queryResult = null;
        try {
            queryResult = twitter.search(query);
        } catch (TwitterException twe) {
            logger.warn("Got exception while connecting with twitter", twe);
            return Collections.<Tweet> emptyList();
        }
        List<Tweet> tweets = queryResult.getTweets();
        return tweets;
    }

}
